#!/bin/csh -f

if ( $#argv < 2 ) then
  echo "Error: provide the binary directory as first argument,"
  echo "and the location of programs.txt as the second."
  echo "A html subdirectory will be created in the current dir."
  exit
endif

set GMXBINDIR   = $1
set PROGFILE	= $2

set dir = $cwd

set VER		= 4.5
set MANDIR	= online
set HTML	= $cwd/html
set HTMLOL	= $HTML/$MANDIR
set HTMLIDX	= $HTML/$MANDIR.html

set GENERAL 	= "getting_started:Getting_Started flow:Flow_Chart files:File_Formats mdp_opt:mdp_options"

cd $GMXBINDIR
set PROGRAMS = [a-z]*
cd $dir

echo ""
echo "Generating table of contents in $HTMLIDX"
echo "-------------------------------------------"

if ( ! -d $HTML ) mkdir $HTML
if ( ! -d $HTMLOL ) mkdir $HTMLOL

if ( -f $HTMLIDX ) \rm $HTMLIDX
touch $HTMLIDX

cat > $HTMLIDX << EOD
<HTML>
<HEAD>
<TITLE>GROMACS $VER Online Reference </TITLE>
</HEAD>
<LINK rel=stylesheet href="online/style.css" type="text/css">
<BODY text="#000000" bgcolor="#FFFFFF" link="#0000FF" vlink="#990000" alink="#FF0000">

<table WIDTH="98%" NOSAVE NOBORDER >
<TR><TD WIDTH=400>
<TABLE WIDTH=400 NOBORDER>
<TD WIDTH=116>
<a href="http://www.gromacs.org/">
<img SRC="images/gmxlogo_small.jpg" BORDER=0 height=133 width=116></a></td>
<td ALIGN=LEFT VALIGN=TOP WIDTH=280>
<br><br>
<h2>
GROMACS 4.5<br>
Online Reference</h2>
</td>
</TABLE></TD>
<td ALIGN=RIGHT VALIGN=BOTTOM WIDTH="*" NOSAVE>
<B>VERSION 4.5<br>
Thu 26 Aug 2010</B></td>
</tr>
</table>

<hr>

<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=10>
<TR>
<TD VALIGN=top WIDTH="25%">
<h3>General</h3>
EOD
foreach i ( $GENERAL )
  set file = `echo $i | cut -d: -f1`
  set desc = `echo $i | cut -d: -f2 | sed 's/_/ /g'`
  echo "<A HREF="\""$MANDIR/"$file".html"\"">"$desc"</a>" >> $HTMLIDX
  echo "<br><br>" >> $HTMLIDX
end
cat >> $HTMLIDX <<EOD
<A HREF="gmxfaq.html">FAQ</a>
<br>
</TD>
<TD VALIGN=top WIDTH=75%>
<h3>Programs</h3>
<multicol cols=5> 
<A HREF="$MANDIR/options.html">Options</a>
<br>
EOD
foreach program ( $PROGRAMS )
  if ( ( -x $GMXBINDIR/$program ) && ( $program != "my_dssp" ) && ( $program != "GMXRC" ) && ( $program != "completion.csh" ) && ( $program != "completion.zsh" ) && ( $program != "average" ) && ( $program != "completion.bash" ) && ( $program != "luck" ) ) then  
    echo "<br><a href="$MANDIR/$program.html">$program</a>" >> $HTMLIDX
  endif
end
cat >> $HTMLIDX << EOD
</multicol>
</TD>
</TR>
</TABLE>
<HR>
<h3>Programs by Topic</h3>
<multicol cols=3>
EOD
awk -F '|' -v mandir=$MANDIR '{\
  if (NF) {\
    if ( $1 == "HEAD" ) {\
      hnr++;\
      printf("<A HREF=\"#HNR%d\">%s</A><br>\n",hnr,$2);\
    }\
  }\
}' $PROGFILE >> $HTMLIDX
echo "</multicol> " >> $HTMLIDX
awk -F '|' -v mandir=$MANDIR '{\
  if (NF) {\
    if ( $1 == "HEAD" ) {\
      hnr++;\
      printf("\n<A NAME=\"HNR%d\">\n",hnr);\
      printf("<TABLE CELLSPACING=1>\n<TR><TD>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n");\
      printf("<TR><TD COLSPAN=2><b>%s</b>\n",$2);\
    } else if ( $1 == "END" ) {\
      printf("</TABLE>\n");\
    } else {\
      printf("<TR><TD><A HREF=\"%s/%s.html\">%s</A></TD><TD>%s</TD>\n",mandir,$1,$1,$2);\
    }\
  }\
}' $PROGFILE >> $HTMLIDX
cat >> $HTMLIDX <<EOD
<p>
<hr>
<div ALIGN=RIGHT>
<font size="-1"><a href="http://www.gromacs.org">http://www.gromacs.org</a></font><br>
</body>
</html>
EOD

echo "Generating html manual for GROMACS programs"
echo "-------------------------------------------"

cd $dir

foreach program ( $PROGRAMS )
  if ( ( -x $GMXBINDIR/$program ) && ( $program != "my_dssp" ) && ( $program != "GMXRC" ) && ( $program != "completion.csh" ) && ( $program != "completion.zsh" ) && ( $program != "average" ) && ( $program != "completion.bash" ) && ( $program != "luck" ) ) then
    echo -n "$program "
    cd $HTMLOL
    $GMXBINDIR/$program -quiet -man html >& /dev/null
    endif
  endif
end

#last line
